﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Messaging;
using log4net;

namespace YogaDictionary.Common
{
    public static class Log
    {
        private static readonly ILog log;

        static Log()
        {
            log = LogManager.GetLogger(typeof(Log));
        }

        private static string GetCallerMethodFullName()
        {
            var frames = (new StackTrace(true)).GetFrames();
            var frame = frames.FirstOrDefault(f => f.GetMethod().DeclaringType != typeof (Log));
            if (frame == null)
                throw new Exception("There was no caller method found");
            var method = frame.GetMethod();
            return method.DeclaringType.FullName + "." + method.Name;
        }

        private static string GetMessageFormat(string message)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0} [{1}] {2}", DateTime.Now, GetCallerMethodFullName(), message);
        }

        #region Public Members

        public static void Fatal(string message)
        {
            if (log.IsFatalEnabled)
                log.Fatal(GetMessageFormat(message));
        }
        public static void Error(string message)
        {
            if (log.IsErrorEnabled)
                log.Error(GetMessageFormat(message));
        }
        public static void Warning(string message)
        {
            if (log.IsWarnEnabled)
                log.Warn(GetMessageFormat(message));
        }
        public static void Info(string message)
        {
            if (log.IsInfoEnabled)
                log.Info(GetMessageFormat(message));
        }

        public static void Debug(string message)
        {
            if (log.IsDebugEnabled)
                log.Debug(GetMessageFormat(message));
        }

        #endregion

    }

}
